/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.data.HttpCacheProxy');
(function () {
	
	/**
	 * An HttpProxy with caching to prevent the need to make the same request twice per page load
	 * @class HttpCacheProxy
	 * @extends net.autodata.data.HttpProxy
	 * @namespace net.autodata.data
	 */
	var HttpCacheProxy = Ext.extend(net.autodata.data.HttpProxy, {
		useCache: true,
		requiredParams: undefined,
		
		/**
		 * @method getCache
		 * @type Ext.util.MixedCollection
		 */
		getCache: function ()
		{	
			var cache;
			return function () {
				if (!cache) {
					cache = new Ext.util.MixedCollection(false);
				}
				return cache;
				
			};
		}(),
		
		/**
	     * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses.  Note: Should only be used by custom-proxy developers.
	     * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
	     * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
	     */
		doRequest: function (action, rs, params, reader, callback, scope, options)
		{	
			//First check cache to see if this has been requested before
			var cacheKey = Ext.urlEncode(params);
			var cache = this.getCache();
			var cacheObj = this.useCache ? cache.get(cacheKey) : undefined;
			
			if (cacheObj === undefined) {
				this.executeRequest(action, rs, params, reader, callback, scope, options);
				return;
			}
						
			var response = cacheObj.data;
			cacheObj.hitCount++;
			ASC.log(String.format('{0} was retrieved from cache; hit count={1}', cacheKey, cacheObj.hitCount));
			this._processResponse(response, params, reader, callback, scope, options, false);
		},
		
		/**
		 * @method processResponse
		 * @private
		 */
		processResponse: function (response, params, reader, callback, scope, options)
		{
			this._processResponse(response, params, reader, callback, scope, options, this.useCache);	
		},
		
		/**
		 * Overloaded immplementation of processResponse
		 * @method _processResponse
		 * @private
		 */
		_processResponse: function (response, params, reader, callback, scope, options, cacheResponse)
		{
			
			var cache = this.getCache();
			
			try {
                var result = reader.readRecords(response);
                
                //If the reader is able to process the response 
                if (this.useCache && cacheResponse !== false) {
                	var cacheKey = Ext.urlEncode(params);
                	cache.add({id: cacheKey, data: response, hitCount: 0});
                	ASC.log(String.format('{0} was added to the cache', cacheKey));
                }
                
            } catch (e) {
                this.fireEvent("loadexception", this, params, response, e);
                callback.call(scope || this, null, options, false);
                return;
            }
            
            this.fireEvent("load", this, response, options);
            callback.call(scope, result, options, true);		
		}
	});
	
	net.autodata.data.HttpCacheProxy = HttpCacheProxy;	
})();